#include "ConfigReader.h"


ConfigReader::ConfigReader(void)
{
}


ConfigReader::~ConfigReader(void)
{
}

// Read the configuration file
// return: -1 failed; 1 succeed
int ConfigReader::ReadConfigFile( string fileName )
{
	ifstream configReader( fileName.c_str() );

	if( configReader == NULL )
	{
		cout<<"error reading configuration file"<<endl;
		return -1;
	}
		
	string line;					
			
	while( getline( configReader, line ) )
	{
		if( line.size() > 0 && line.at( 0 ) != '#' )
		{
			if( AnalyzeParameters( line ) == -1 )
				return -1;
		}
	}

	return 1;
}

// anylize the parameters
// return: -1 if failed
int ConfigReader::AnalyzeParameters( string line ){
	// split the line according to "="
	int pos = line.find( "=" );
	if( pos == -1 )
		return pos;

	string propertyName = line.substr( 0, pos );
	string value = line.substr( pos + 1, line.length() - pos - 1 );
	if( propertyName == "graph_type" ){
		if( value == "velvet" )
			Configure::GRAPH_TYPE = VELVET;
		else if( value == "soap" )
			Configure::GRAPH_TYPE = SOAP;
		else{
			cout<<"Error: The graph_type parameter is not correct.\n";
			return -1;
		}
	}
	else if( propertyName == "data_directory" ){
		Configure::DATA_DIRECTORY = value;
	}
	else if( propertyName == "output_folder" ){
		Configure::OUTPUT_FOLDER = value;
		mkdir (Configure::OUTPUT_FOLDER.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
		if( Configure::OUTPUT_FOLDER.substr( Configure::OUTPUT_FOLDER.length() - 1, 1 ) != Configure::DIRECTORY_SIGN )
			Configure::OUTPUT_FOLDER += Configure::DIRECTORY_SIGN;
	}
	else if( propertyName == "scaffolds_file" ){
		Configure::SCAFFOLDS_FILE = value;
	}
	else{
		cout<<"This line is wrong: "<<line<<endl;
		return -1;
	}

	return 1;
}